/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.service.GenericService;

import java.io.IOException;

public class Application {
    /**
     * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
     * launch the application
     */
    public static void main(String[] args) throws IOException {
//        testAsync();
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        ApplicationConfig application = new ApplicationConfig("dubbo-demo-api-consumer");
        application.setQosEnable(false);
        reference.setApplication(application);
        reference.setRegistry(new RegistryConfig("N/A"));
        reference.setInterface("org.apache.dubbo.demo.DemoService");//声明接口名
        reference.setGeneric(true);//声明为泛化接口
        reference.setAsync(false);
        reference.setUrl("10.10.16.102:20880");
//        if (StringUtils.isNotEmpty(serviceVersion)) {
//            reference.setVersion(serviceVersion);
//        }


        // ReferenceConfig实例很重，封装了与注册中心的连接以及与提供者的连接，
        // 需要缓存，否则重复生成ReferenceConfig可能造成性能问题并且会有内存和连接泄漏。
        // API方式编程时，容易忽略此问题。这里使用dubbo内置的简单缓存工具类进行缓存
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);

        //设置参数和参数类型 
        String[] parameterTypes = {"java.lang.String"};
        Object[] parameterValues = {"hello"};

        Object result = genericService.$invoke("sayHello", parameterTypes, parameterValues);
        System.out.println(result);

    }

    public static void testAsync() throws IOException {
        ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
        ApplicationConfig application = new ApplicationConfig("dubbo-demo-api-consumer");
        application.setQosEnable(false);
        reference.setApplication(application);
//        reference.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
        reference.setRegistry(new RegistryConfig("N/A"));
        reference.setInterface(DemoService.class);
        reference.setUrl("10.10.16.102:20880");
        reference.setAsync(true);
        DemoService service = reference.get();
        service.sayHelloFuture("dubbo").thenAccept(System.out::println);

        System.in.read();
    }
}
